﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using ImApiDotNet;
using System.Data.OracleClient;
using ShareMarking.Interface;

namespace ShareMarking.NC.Auto
{
    public partial class Main : Form
    {
        #region 属性========================================


        #endregion

        #region Form设置====================================

        public Main()
        {
            InitializeComponent();
        }

        public static readonly string messagebox_title = "温馨提示";
        public static readonly string close_tip = "NC同步服务正在运行,\r\n确定退出系统?";

        private void Main_Load(object sender, EventArgs e)
        {
            notifyIcon.Visible = true; //显示托盘图标
            this.ShowIcon = true; //应用程序窗口是否显示图标
            this.ShowInTaskbar = true; //是否在windows任务栏显示图标
            this.WindowState = FormWindowState.Normal;  //设置窗口的显示状态（最大化、最小化和普通模式）

            if(!Directory.Exists("Log/"))
            {
                Directory.CreateDirectory("Log/");
            }
        }
        
        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            ExitToolStripMenuItem.Enabled = false;  //单击退出后退出选项为不可用
            if (MessageBox.Show(close_tip, messagebox_title, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                System.GC.Collect();
                //this.Close();
                //Application.Exit();//整个应用程序退出 
            }
            else
            {
                ExitToolStripMenuItem.Enabled = true;
                e.Cancel = true;
            }
        }
        
        private void MainToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Main_SizeChanged(object sender, EventArgs e)
        {
            bool isMin = this.WindowState == FormWindowState.Minimized;//視窗是否為最小化
            this.notifyIcon.Visible = isMin;//是否顯示在系統匣
            this.ShowInTaskbar = !isMin;//是否顯示在工作列
            if (isMin)
            {
                this.Hide();//隱藏主視窗
                this.notifyIcon.ShowBalloonTip(100);//氣球提示2秒鐘
            }
        }
        
        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" [美赞臣]NC数据同步\r\n powerby-小马科技", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
        }

        #endregion

        private void btn_sync_Click(object sender, EventArgs e)
        {
            //获取所有NC数量
            string httpstr = "http://ncsearch.baiytfp.com";  //正式
            // string httpstr = "http://nc.baiytfp.com";
            string access_key_id = "xiaoma";
            string access_key_secret = "xiaoma_secret";
            var ncsync = new NC_Sync(httpstr, access_key_id, access_key_secret);

            var count = ncsync.GetNCCount();
            var pagesize = 50;
            var currentpage = 200;
            var nextpage = 201;
            var pagecout = (int)(count / pagesize);
            for (int i = 0; i < pagecout; i++)
            {
                ncsync.GetNC(currentpage, nextpage, pagesize, count);
                currentpage += 1;
                nextpage += 1;
            }
        }

        private void btn_manage_Click(object sender, EventArgs e)
        {
            //new qrCodeManage().ManageNCQr();
        }

        #region 定时器======================================

        #endregion
        
    }
}